﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace An_Im.Theme.Custom
{

    /// <summary>
    /// 解决了选中劫持的问题，不会因此而跳转影响访问体验。
    /// https://social.msdn.microsoft.com/Forums/vstudio/en-US/bab3fb62-bf9e-4db6-854c-829bd2896319/how-to-prevent-scrollviewer-from-calling-bringintoview?forum=wpf
    /// </summary>
    public class AnExtendedScrollViewer : ScrollViewer
    {
        protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
        {
            var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta);
            eventArg.RoutedEvent = UIElement.MouseWheelEvent;
            eventArg.Source = e.Source;

            ScrollViewer scv = (ScrollViewer)this;
            scv.RaiseEvent(eventArg);
            e.Handled = true;
            base.OnPreviewMouseWheel(e);
        }

        public override void OnApplyTemplate()
        {
            var presenter = this.GetTemplateChild("PART_ScrollContentPresenter") as ScrollContentPresenter;
            if (presenter != null)
            {
                presenter.RequestBringIntoView += Presenter_RequestBringIntoView;
            }
            base.OnApplyTemplate();
        }

        private void Presenter_RequestBringIntoView(object sender, System.Windows.RequestBringIntoViewEventArgs e)
        {
            e.Handled = true;
        }

        //protected override void OnMouseWheel(MouseWheelEventArgs e)
        //{
        //    if (ViewportHeight + VerticalOffset >= ExtentHeight && e.Delta <= 0)
        //        return;

        //    if (VerticalOffset == 0 && e.Delta >= 0)
        //        return;

        //    base.OnMouseWheel(e);
        //}



    }
}
